<template>
    <div class="row">
        <div class="col-md-6">
            <p>
                <button @click="addParent()" class="btn btn-white btn-default btn-round">
                    <i class="ace-icon fa  fa-pencil-square-o red2"></i>
                    添加一级分类
                </button>
                &nbsp;
                <button @click="getAllCategory()" class="btn btn-white btn-default btn-round">
                    <i class="ace-icon fa   fa-arrow-circle-o-down red2"></i>
                    刷新
                </button>
            </p>
            <table id="parent-simple-table" class="table  table-bordered table-hover">
                <thead>
                <tr>
                    <th>父id</th>
                    <th>名称</th>
                    <th>顺序</th>
                    <th>操作</th>
                </tr>
                </thead>

                <tbody>
                <tr v-for="category in parentCategorys" @click="clickParent(category)" v-bind:class="category.id === selectedCategory.id ? 'active' : ''">
                    <td>{{category.parent}}</td>
                    <td>{{category.name}}</td>
                    <td>{{category.sort}}</td>
                    <td>
                        <div class="hidden-sm hidden-xs btn-group">
                            <button @click="update(category)" class="btn btn-xs btn-info">
                                <i class="ace-icon fa fa-pencil bigger-120"></i>
                            </button>

                            <button @click="del(category.id)" class="btn btn-xs btn-danger">
                                <i class="ace-icon fa fa-trash-o bigger-120"></i>
                            </button>
                        </div>

                        <div class="hidden-md hidden-lg">
                            <div class="inline pos-rel">
                                <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
                                    <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
                                </button>

                                <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                                    <li>
                                        <a href="#" class="tooltip-info" data-rel="tooltip" title="View">
																			<span class="blue">
																				<i class="ace-icon fa fa-search-plus bigger-120"></i>
																			</span>
                                        </a>
                                    </li>

                                    <li>
                                        <a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
																			<span class="green">
																				<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
																			</span>
                                        </a>
                                    </li>

                                    <li>
                                        <a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
																			<span class="red">
																				<i class="ace-icon fa fa-trash-o bigger-120"></i>
																			</span>
                                        </a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
        <div class="col-md-6">
            <p>
                <button @click="addChildren()" class="btn btn-white btn-default btn-round">
                    <i class="ace-icon fa  fa-pencil-square-o red2"></i>
                    添加二级分类
                </button>
            </p>
            <table id="simple-table" class="table  table-bordered table-hover">
                <thead>
                <tr>
                    <th>父id</th>
                    <th>名称</th>
                    <th>顺序</th>
                    <th>操作</th>
                </tr>
                </thead>

                <tbody>
                <tr v-for="category in childenCategorys">
                    <td>{{category.parent}}</td>
                    <td>{{category.name}}</td>
                    <td>{{category.sort}}</td>
                    <td>
                        <div class="hidden-sm hidden-xs btn-group">
                            <button @click="update(category)" class="btn btn-xs btn-info">
                                <i class="ace-icon fa fa-pencil bigger-120"></i>
                            </button>

                            <button @click="del(category.id)" class="btn btn-xs btn-danger">
                                <i class="ace-icon fa fa-trash-o bigger-120"></i>
                            </button>
                        </div>

                        <div class="hidden-md hidden-lg">
                            <div class="inline pos-rel">
                                <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
                                    <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
                                </button>

                                <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                                    <li>
                                        <a href="#" class="tooltip-info" data-rel="tooltip" title="View">
																			<span class="blue">
																				<i class="ace-icon fa fa-search-plus bigger-120"></i>
																			</span>
                                        </a>
                                    </li>

                                    <li>
                                        <a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
																			<span class="green">
																				<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
																			</span>
                                        </a>
                                    </li>

                                    <li>
                                        <a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
																			<span class="red">
																				<i class="ace-icon fa fa-trash-o bigger-120"></i>
																			</span>
                                        </a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
        <div id="form-modal" class="modal fade" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">分类表单</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">父id</label>
                                            <div class="col-sm-10">
                                                <input disabled="disabled" v-model="category.parent" class="form-control">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">名称</label>
                                            <div class="col-sm-10">
                                                <input v-model="category.name" class="form-control">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">顺序</label>
                                            <div class="col-sm-10">
                                                <input v-model="category.sort" class="form-control">
                                            </div>
                                        </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button v-on:click="save()" type="button" class="btn btn-primary">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>
    </div>
</template>
<script>
    export default {
        name: 'category',
        mounted() {//在组件内容被渲染到页面之【后】立即执行的函数
            let _this = this;
            $('body').attr('class', 'no-skin');
            _this.getAllCategory();
        },
        data(){
            return{
                categorys:[],
                parentCategorys:[],
                childenCategorys:[],
                category:{},
                selectedCategory:{}
            }
        },
        methods:{
            //进行Axios异步通讯
            getAllCategory(){
                //加载框显示
                Loading.show();
                let _this = this;
                _this.$ajax.post(process.env.VUE_APP_SERVER+"/business/category/getAllCategorys").then(function (response) {
                    let responseBody = response.data;//获取到axios响应的结果对象
                    _this.categorys = responseBody.categorys;//获取查询的结果
                    // 将所有记录格式化成树形结构
                    _this.parentCategorys = [];
                    for (let i = 0; i < _this.categorys.length; i++) {
                        let c = _this.categorys[i];
                        if (c.parent === '00000000') {
                            _this.parentCategorys.push(c);
                            for (let j = 0; j < _this.categorys.length; j++) {
                                let child = _this.categorys[j];
                                if (child.parent === c.id) {
                                    if (Tool.isEmpty(c.children)) {
                                        c.children = [];
                                    }
                                    c.children.push(child);
                                }
                            }
                        }
                    }
                    //清空选中数据
                    _this.selectedCategory = {};
                    //加载框显示
                    Loading.hide();
                });
            },
            //添加一级分类操
            addParent(){
                let _this = this;
                _this.category = {parent:'00000000'};
                //清空选中数据
                _this.selectedCategory = {};
                //弹出添加窗口
                $("#form-modal").modal("show");
            },
            //添加二级分类
            addChildren(){
                let _this = this;
                if(Tool.isEmpty(_this.selectedCategory)){
                    Toast.warning("请选择一级分类!")
                    return;
                }
                //获取选中的一级分类的id给二级分类父Id进行赋值
                _this.category = {parent: _this.selectedCategory.id};
                //弹出添加窗口
                $("#form-modal").modal("show");
            },
            //更新操作
            update(category){
                let _this = this;
                //获取选中行的数据
                _this.category = $.extend({}, category);
                //弹出添加窗口
                $("#form-modal").modal("show");
            },
            //调用后台接口完成数据的添加和更新
            save(){
                let _this = this;
                _this.$ajax.post(process.env.VUE_APP_SERVER+"/business/category/save",_this.category).then(function (response) {
                    let responseBody = response.data;//获取到axios响应的结果对象
                    //将弹出框消失
                    $("#form-modal").modal("hide");
                    //弹出接口返回的提示
                    Toast.success(responseBody.msg);//获取查询的结果
                    //刷新页面
                    _this.getAllCategory();
                });
            },
            clickParent(category){
                let _this = this;
                _this.selectedCategory = category;
                _this.childenCategorys = category.children;
            },
            //删除数据
            del(id){
                let _this = this;
                //确认框
                Confirm.show("数据删除后将无法恢复,确定删除吗?",function(){
                    _this.$ajax.get(process.env.VUE_APP_SERVER+"/business/category/deleteCategory?id="+id).then(function (response) {
                        let responseBody = response.data;//获取到axios响应的结果对象
                        //弹出接口返回的提示
                        Toast.success(responseBody.msg);//获取查询的结果
                        //刷新页面
                        _this.getAllCategory();
                    });
                });

            }
        }
    }
</script>
